<?php defined('SYSPATH') OR die('No direct access allowed.');

class Editor_Plugin extends WB_FormItem {
	
	protected $editor_options;
	
	public function __construct($name='', $params=array())
	{
		parent::__construct($name, $params);
		
		$this->editor_options = empty($params['editor_options']) ? array() : $params['editor_options'];
		$this->editor_options['language'] = empty($this->editor_options['language']) ? 
				substr(Kohana::config('locale.language.0'), 0, 2) : 
				$this->editor_options['language'];
		
		Event::add('wb_controller.pre_render_form', array($this, 'init_form'));
	}
	
	function prep_db()
	{
		return str_replace(array('&nbsp;'), array('&amp;nbsp;'), $this->value);
	}
	
	
	public function init_form()
	{
		Kohana::instance()->wb_template->js('plugins/tiny_mce/tiny_mce');
		Kohana::instance()->wb_template->js('plugins/tiny_mce/convert_word');
		Kohana::instance()->wb_template->script('tiny_mce_init_'.$this->name, $this->buildEditorInit());
	}
	
	function buildEditorInit()
	{
		// by http://wiki.moxiecode.com/index.php/TinyMCE:Configuration
		$editor_options = array(
			// GENERAL
				'accessibility_focus' => false,
				//'ask' => true,
				'auto_resize' => false,
				//'debug' => true,
				//'directionality ' => 'rtl',
				//'docs_language' => 'hu',
				//'hide_selects_on_submit' => true,
				//'language' => 'hu',
				//'theme' => 'o2k7',
				//'skin_variant' => 'silver',				//or 'black'
				'strict_loading_mode' => false,		//makes loading slower!
				//'table_inline_editing' => true,
				
			// CLEANUP
				//'apply_source_formatting' => false,
				'convert_fonts_to_spans' => true,
				'convert_newlines_to_brs' => true,
				//'doctype' => '',
				//'encoding' => 'xml',
				'entity_encoding' => 'raw',				//or 'numeric' or 'named'
				//'extended_valid_elements' => 'img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name]',
				//'fix_content_duplication' => false,
				//'fix_list_elements' => true,
				//'fix_table_elements' => true,
				//'fix_nesting' => true,
				//'font_size_classes' => 'fontSize1,fontSize2,fontSize3,fontSize4,fontSize5,fontSize6,fontSize7',
				//'font_size_style_values' => 'xx-small,x-small,small,medium,large,x-large,xx-large',
				'force_p_newlines' => true,
				'force_br_newlines' => false,
				'force_hex_style_colors' => true,
				//'forced_root_block' => 'p',
				//'indentation' => '20px',
				//'inline_styles' => false,
				//'invalid_elements' => 'strong,em',
				'preformatted' => false,
				//'remove_trailing_nbsp' => true,
				'trim_span_elements' => false,
				//'convert_urls' => false,
				'relative_urls' => false,
				'remove_script_host' => false,
				'document_base_url' => url::base(),
				'paste_insert_word_content_callback' => 'convertWord',
		
			
			// LAYOUT
				//'theme_advanced_toolbar_align' => 'left',
				'theme_advanced_toolbar_location' => 'top',
			
			// PLUGINS
				'plugins' => 'advimage, advlink, emotions, fullscreen, safari, paste, searchreplace, table, media',
				'theme_advanced_buttons1_add' => 'fontselect, fontsizeselect, search,replace',
				'theme_advanced_buttons2_add_before' => 'cut, copy, pastetext, pasteword',
				'theme_advanced_buttons2_add' => 'help, cleanup, code, forecolor, backcolor, sub, sup',
				'theme_advanced_buttons3_add_before' => 'tablecontrols',
				'theme_advanced_buttons3_add' => 'emotions, fullscreen, media',
				'theme_advanced_disable' => 'styleselect',
				//'plugin_preview_pageurl' => '../preview.php',					//plugin: preview
				//'plugin_preview_width' => 550,								//plugin: preview
				//'plugin_preview_height' => 600,								//plugin: preview
				//'theme_advanced_buttons1_add' => 'cite,ins,del,abbr,acronym',	//plugin: xhtmlxtras
				//'theme_advanced_buttons2_add' => 'visualchars',				//plugin: visualchars
				//'theme_advanced_buttons1_add' => 'advhr',						//plugin: advhr
				//'extended_valid_elements' => 'hr[class|width|size|noshade]',	//plugin: preview
				'file_browser_callback' => 'ajaxfilemanager'
			);
		foreach ($this->editor_options as $k => $v) $editor_options[$k] = $v;
		
		$js = "\ntinyMCE.init({\n	theme : \"advanced\",\n	mode : \"exact\",\n	elements: \"".$this->name."\"";
		foreach ($editor_options as $k => $v) {
			if (is_bool($v)) $js .= ",\n	$k: ".($v ? 'true' : 'false');
			elseif (is_numeric($v)) $js .= ",\n	$k: $v";
			elseif (in_array($k, array('content_css', 'editor_css', 'popup_css')))
				$js .= ",\n	$k: \"".url::base()."$v\"";
			else $js .= ",\n	$k: \"$v\"";
		}
		$afm_url = '../../../../plugins/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php';
		if ( ! empty($this->editor_options['language'])) $afm_url .= '?language='.$this->editor_options['language'];
		$js .= "\n});";
		$js .= '
		function ajaxfilemanager(field_name, url, type, win) {
			var ajaxfilemanagerurl = "'.$afm_url.'";
			switch (type) {
				case "image":
					break;
				case "media":
					break;
				case "flash": 
					break;
				case "file":
					break;
				default:
					return false;
			}
			tinyMCE.activeEditor.windowManager.open({
				url: "'.$afm_url.'",
				width: 782,
				height: 440,
				inline : "yes",
				close_previous : "no"
			},{
				window : win,
				input : field_name
			});
		}';
		return $js;
	}

	
	/**
	 * Renders the form element
	 * 
	 * @return string
	 */
	public function render_item()
	{
		
		$str = $this->prefix;
		$str .= form::label($this->name, $this->label);
		$str .= form::textarea($this->name, $this->value, html::attributes($this->attributes));
		if ($this->description) $str .= '<span class="description">'.$this->description.'</span>';
		$str .= $this->suffix;
		
		return $str;
	}
	
}

//End of Editor_Plugin Library